﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Antioch.Base.PlayerStates
{
    internal class Stand : IState
    {
        public void Enter(StateMachine Machine)
        {
            Machine.DrawComponent.Play("Stand", true);
            Machine.PhysicsComponent.PhysicsBody.GetFixtureList().SetFriction(0.8f);
        }

        public void Leave(StateMachine Machine)
        {
            
        }

        public void Update(StateMachine Machine)
        {
            

            //if (Machine.Input.Hurt)
            //{
            //    Machine.Transition(typeof(Flinch));
            //    return;
            //}

            if (!Machine.PhysicsComponent.IsOnGround)
            {
                Machine.Transition(typeof(Fall));
                return;
            }

            if (Machine.Input.JumpPressed)
            {
                Machine.Transition(typeof(Jump));
                return;
            }

            if (Machine.Input.RawAimStick.X > 0)
                Machine.PlayerEntity.Facing = Jemgine.Data.Facings.Right;
            else if (Machine.Input.RawAimStick.X < 0)
                Machine.PlayerEntity.Facing = Jemgine.Data.Facings.Left;

            if (Machine.Input.NavigationStick.X != 0)
            {
                Machine.Transition(typeof(Run));
                return;
            }

            /*
            if (Machine.Input.NavigationStick.X > 0)
            {
                Machine.PlayerEntity.Facing = Jemgine.Data.Facings.Right;
                Machine.Transition(typeof(Run));
            }
            else if (Machine.Input.NavigationStick.X < 0)
            {
                Machine.PlayerEntity.Facing = Jemgine.Data.Facings.Left;
                Machine.Transition(typeof(Run));
            }
            else if (Machine.PhysicsComponent.IsOnGround)
            {
                float FloorAngle = Jemgine.Math.Vector.AngleBetweenVectors(
                    Jemgine.Math.Vector.Normalize(-Machine.PhysicsComponent.Gravity),
                    Machine.PhysicsComponent.GroundNormal);

                if (System.Math.Abs(FloorAngle) > System.Math.PI / 5.0)
                {
                    Machine.Transition(typeof(SlopeSlide));
                    return;
                }

                Machine.Memory.PushPullPoint = null;
                Vector2 GrabSpot = new Vector2(0, 0);
                GrabSpot = Machine.PlayerEntity.TransformToWorldSpace(GrabSpot);

                Jemgine.Math.OBB GrabBox = new Jemgine.Math.OBB();
                GrabBox.Center = GrabSpot;
                GrabBox.Extents = Machine.PhysicsComponent.Dimensions / 2;
                GrabBox.Angle = Machine.PhysicsComponent.PhysicsBody.GetAngle();

                List<PlayerNavigationNode> HitNodes = new List<PlayerNavigationNode>();

                if (Machine.NavigationModule != null &&
                    Machine.NavigationModule.FindCollidingNodes(GrabBox, (A) => HitNodes.Add(A)) > 0)
                {
                    PushPullGrabPoint GrabPoint = null;
                    foreach (var Node in HitNodes) if (Node is PushPullGrabPoint) GrabPoint = Node as PushPullGrabPoint;

                    if (GrabPoint != null && GrabPoint.CanGrab(Machine.PlayerEntity))
                    {
                        Machine.Memory.PushPullPoint = GrabPoint;
                        Machine.PlayerEntity.Sim.GetModule<ActionIconModule>().
                            ActivateIcon(Machine.PlayerEntity.Sim.EpisodeContent.Load<Texture2D>(
                                "controller/xboxControllerButtonY"), GrabPoint.GetWorldPosition());
                    }
                }

                if (Machine.Input.UsePressed && Machine.Memory.PushPullPoint != null)
                {
                    Machine.Transition(typeof(Grab));

                }
            }*/
        }
    }
}
